package com.marvels.approve.process.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.marvels.approve.process.dto.AssistantDTO;
import com.marvels.approve.process.dto.FaceSigAppointmentDTO;
import com.marvels.approve.process.dto.FaceSignApiDTO;
import com.marvels.approve.process.dto.FacesigOperationDTO;
import com.marvels.approve.process.dto.RoomLogDTO;
import com.marvels.approve.process.service.IFaceCheckAssignmentService;
import com.marvels.approve.process.service.IFaceSigService;
import com.marvels.common.dto.MarvelsApiResultDTO;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;

/**
 * 视频面签对外接口
 * @author zhaosa
 *
 */
@RequestMapping("/approve/spmq/api")
@RestController
public class FaceSigAppointmentApiController {
	@Autowired
	private IFaceSigService iFaceSigService;
	@Autowired
	private IFaceCheckAssignmentService faceCheckAssignmentService;
	@Value("${spring.application.name}")
	private String appName;
	/**
	 * 客户经理获得签名
	 * @param paramMap
	 * @return
	 */
	@RequestMapping("/getTencentSig")
	@ResponseBody
	public MarvelsApiResultDTO getTencentSig(@RequestBody FaceSignApiDTO faceSignApiDto){
		Map<String, Object> result = null;
		try {
			result = iFaceSigService.getTencentSig(faceSignApiDto);
			MarvelsLogUtil.getInstance().info("客户经理获得签名返回结果=======》》》"+result);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================客户经理获得签名失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================客户经理获得签名异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),result,appName,"");
	}
	/**
	 * 预约
	 * @param paramMap
	 * @return
	 */
	@RequestMapping("/appointment")
	@ResponseBody
	public MarvelsApiResultDTO appointment(@RequestBody FaceSignApiDTO faceSignApiDto){
		try {
			iFaceSigService.appointment(faceSignApiDto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================面签预约失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================面签预约异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),null,appName,"");
	}
	/**
	 *  取消预约
	 * @param idFintechUmUser
	 * @param accessToken
	 * @param date
	 * @param timeSlotId
	 * @param loanId
	 * @param cid
	 * @return
	 */
	@RequestMapping("/cancelAppointment")
	@ResponseBody
	public MarvelsApiResultDTO cancelAppointment(@RequestBody FaceSignApiDTO faceSignApiDto){
		try {
			iFaceSigService.cancelAppointment(faceSignApiDto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================取消面签预约失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================取消面签预约异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),null,appName,"");
	}
	/**
	 * 获取时间列表
	 * @param timestamp
	 * @return
	 */
	@RequestMapping("/getFaceSigTimeConfByDate")
	@ResponseBody
	public MarvelsApiResultDTO getFaceSigTimeConfByDate(@RequestBody FaceSignApiDTO faceSignApiDto){
		List<Map<String,Object>> result = null;
		try {
			result = iFaceSigService.getFaceSigTimeConfByDate(faceSignApiDto);
			MarvelsLogUtil.getInstance().info("获取时间列表返回结果=======》》》"+result);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================获取时间列表失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================获取时间列表异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),result,appName,"");
	}
	/**
	 * 获取未预约列表
	 * @param paramMap
	 * @return
	 */
	@RequestMapping("/queryLoanList")
	@ResponseBody
	public MarvelsApiResultDTO queryLoanList(@RequestBody FaceSignApiDTO faceSignApiDto){
		Map<String,Object> result = null;
		try {
			result = iFaceSigService.queryLoanList(faceSignApiDto);
			MarvelsLogUtil.getInstance().info("获取未预约列表返回结果=======》》》"+result);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================获取未预约列表失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================获取未预约列表异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),result,appName,"");
	}
	/**
	 * 获取已预约列表
	 * @param paramMap
	 * @return
	 */
	@RequestMapping("/appointmentByAppointmentList")
	@ResponseBody
	public MarvelsApiResultDTO appointmentByAppointmentList(@RequestBody FaceSignApiDTO faceSignApiDto){
		Map<String, Object> result = null;
		try {
			result = iFaceSigService.appointmentByAppointmentList(faceSignApiDto);
			MarvelsLogUtil.getInstance().info("获取已预约列表返回结果=======》》》"+result);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================获取已预约列表失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================获取已预约列表异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),result,appName,"");
	}
	/**
	 * 分配助理
	 * @param assistantDto
	 * @return
	 */
	@RequestMapping("/allotAssistant")
	@ResponseBody
	public MarvelsApiResultDTO allotAssistant(@RequestBody AssistantDTO assistantDto){
		String result = null;
		try {
			result = iFaceSigService.allotAssistant(assistantDto);
			MarvelsLogUtil.getInstance().info("分配助理返回结果=======》》》"+result);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================分配助理失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================分配助理异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),null,appName,"");
	}
	/**
	 * 获取助理列表
	 * @param idFintechUmUser
	 * @param accessToken
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/getAssistant")
	@ResponseBody
	public MarvelsApiResultDTO getAssistant(@RequestBody FaceSignApiDTO faceSignApiDto){
		Map<String,Object> result = null;
		try {
			result = iFaceSigService.getAssistant(faceSignApiDto);
			MarvelsLogUtil.getInstance().info("获取助理列表返回结果=======》》》"+result);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================获取助理列表失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================获取助理列表异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),result,appName,"");
	}
	/**
	 * 取消分配助理
	 * @param idFintechUmUser
	 * @param accessToken
	 * @param appointmentId
	 * @return
	 */
	@RequestMapping("/cancelAllot")
	@ResponseBody
	public MarvelsApiResultDTO cancelAllot(@RequestBody FaceSignApiDTO faceSignApiDto){
		try {
			iFaceSigService.cancelAllot(faceSignApiDto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================取消分配助理失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================取消分配助理异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),null,appName,"");
	}
	/**
	 * 加入房间日志
	 * @param roomLogDto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addRoomLog")
	public MarvelsApiResultDTO  addRoomLog(@RequestBody RoomLogDTO roomLogDto){
		try {
			iFaceSigService.insertRoomLog(roomLogDto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================添加room日志失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================添加room日志异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),null,appName,"");
	}
	/**
	 * 修改面签状态
	 * @param faceSigAppointmentDTO
	 * @return
	 */
	@RequestMapping("/modifyAppointMentStatus")
	@ResponseBody
	public MarvelsApiResultDTO  modifyAppointMentStatus(@RequestBody FaceSigAppointmentDTO faceSigAppointmentDTO){
		try {
			iFaceSigService.modifyAppointMentStatus(faceSigAppointmentDTO);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================修改面签状态失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================修改面签状态异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),null,appName,"");
	}
	/**
	 * 签合同
	 * @param faceSigAppointmentDTO
	 * @return
	 */
	@RequestMapping("/compactSign")
	@ResponseBody
	public MarvelsApiResultDTO compactSign(@RequestBody FacesigOperationDTO facesigOperationDto){
		try {
			faceCheckAssignmentService.resetCompactSign(facesigOperationDto, false);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================签合同失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================签合同异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),null,appName,"");
	}
	/**
	 * 获取坐席正在面签的的预约和助理获取
	 * @param faceSignApiDto
	 * @return
	 */
	@RequestMapping("/queryFaceSiging")
	@ResponseBody
	public MarvelsApiResultDTO queryFaceSiging(@RequestBody FaceSignApiDTO faceSignApiDto){
		Map<String,Object> result = null;
		try {
			result = iFaceSigService.queryFaceSiging(faceSignApiDto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================获取坐席正在面签的的预约和助理获取失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================获取坐席正在面签的的预约和助理获取异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),result,appName,"");
	}
	/**
	 * 坐席获得签名以及相关登录信息
	 * @param faceSignApiDto
	 * @return
	 */
	@RequestMapping("/getTencentSigByFaceJobId")
	@ResponseBody
	public MarvelsApiResultDTO getTencentSigByFaceJobId(@RequestBody FaceSignApiDTO faceSignApiDto){
		Map<String,Object> result = null;
		try {
			result = iFaceSigService.getTencentSigByFaceJobId(faceSignApiDto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================坐席获得签名以及相关登录信息失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================坐席获得签名以及相关登录信息异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),result,appName,"");
	}
	/**
	 * 登录
	 * @param user
	 * @return
	 */
	@RequestMapping("/faceJobLogin")
	@ResponseBody
	public MarvelsApiResultDTO faceJobLogin(@RequestBody UserDTO user, HttpServletRequest request){
		String userCode = null;
		try {
			userCode = iFaceSigService.faceJobLogin(user, request);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================app登录失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================app登录异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),userCode,appName,"");
	}
	/**
	 * 预览合同
	 * @param faceSignApiDto
	 * @return
	 */
	@RequestMapping("/html")
	@ResponseBody
	public MarvelsApiResultDTO html(@RequestBody FaceSignApiDTO faceSignApiDto){
		String html = null;
		try {
			html = iFaceSigService.html(faceSignApiDto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================app预览合同失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================app预览合同异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),html,appName,"");
	}
}
